package com.he.controller;

import com.he.pojo.Books;
import com.he.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
@SuppressWarnings("All")
public class BookController {
    //controller调用Service层

    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    //查询全部的书籍，并且返回到一个书籍展示页面

    @RequestMapping("allBook")
    public String list(Model model){
        List<Books> list = bookService.queryAllBook();
        model.addAttribute("list",list);
        return "allBook";
    }

    //跳转增加书籍页面
    @RequestMapping("/toAddBook")
    public String toAddPaper(){
        return "addBook";
    }

    //添加书籍的请求
    @RequestMapping("/addBook")
    public String addBook(Books books){
        System.out.println("addBook=>" +books);
        bookService.addBook(books);
        return "redirect:/book/allBook";
        //重定向到allbook请求
    }

    //跳转修改页面
    @RequestMapping("/toUpdateBook")
    public String toUpdatePaper(int id,Model model){
        Books books = bookService.queryBookById(id);
        model.addAttribute("QBooks",books);
        return "updateBook";
    }

    //修改书籍
    @RequestMapping("/updateBooks")
    public String updateBook(Books book){
        System.out.println("updateBook=>"+book);
        bookService.updateBook(book);
        return "redirect:/book/allBook";
    }

    //跳转删除页面
    @RequestMapping("/toDeleteBook")
    public String toDeletePaper(int id,Model model){
        Books books = bookService.queryBookById(id);
        model.addAttribute("DBooks",books);
        return "deleteBook";
    }

    //删除书籍
    @RequestMapping("/deleteBooks")
    public String deleteBook(Books books){
        bookService.deleteBookById(books.getBookID());
        return "redirect:/book/allBook";

    }

    //跳转查询书籍页面
    @RequestMapping("/toQueryBook")
    public String toQuerypaper(String queryBookName,Model model){
        Books books = bookService.queryBookByName(queryBookName);
        System.err.println("books"+books);
        List<Books> list = new ArrayList<Books>();
        list.add(books);
        if(books==null){
             list= bookService.queryAllBook();
             model.addAttribute("error","未查到");
        }
        model.addAttribute("list",list);
        return "allBook";

    }


}
